import { level, edit } from '../services/warehouse';
import { user } from '../utils/user';

export default {
  namespace: 'warehouse',
  state: {
    defaultWid: null, // 用户所属的仓库id
    warehouse: [],
  },
  effects: {
    // 查询所有从库信息
    *queryChild({ payload: { onEnd } }, { put }) {
      const info = yield user();
      let result;
      try {
        result = yield level(info.wid);
      } catch (e) {
        return onEnd(`获取信息失败,请刷新重试:${e.message}` || e);
      }
      onEnd(null, result);
      yield put({
        type: 'save',
        payload: {
          defaultWid: info.wid,
          warehouse: result,
        },
      });
    },
    // 编辑某一个仓库
    *edit({ payload: { value, onEnd } }) {
      try {
        yield edit(value);
      } catch (e) {
        return onEnd(e.message || e);
      }
      onEnd();
    },
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },
};

